# 遍历文件，整理出每一个文件的依赖
import os

import javalang

from src.parse_project.entity.depend_data import DependData
from typing import List
from src.tools.Translated import Translated
from src.tools.extractCode import extract


# 主函数

def finishing_depend_main(controller_list, service_list, mapper_list, dao_list) -> List[DependData]:
    """
    根据路径获取所有文件的依赖关系

    :param controller_list: controller文件路径集合
    :param service_list: service文件路径集合
    :param mapper_list: mapper文件路径集合
    :param dao_list: dao文件路径集合
    :return:Depend_data :根据代码文件返回的依赖集
    """
    # 返回的依赖类
    Depend_data: List[DependData] = []
    for item in controller_list:
        Depend_data.append(Create_entity(item))
    for item in service_list:
        Depend_data.append(Create_entity(item))
    for item in mapper_list:
        Depend_data.append(Create_entity(item))
    for item in dao_list:
        Depend_data.append(Create_entity(item))

    return Depend_data


# 根据路径创建实体
def Create_entity(path) -> DependData:
    """
    提取出每一个文件的所有继承的类

    :param path: 每个文件的路径
    :return: 返回每个文件的依赖类
    """
    path = Translated(path)
    # 获取文件的名字
    name: str = os.path.basename(path)
    # 提取源码
    code = extract(path)
    # 创建javalang的语法树
    tree = javalang.parse.parse(code)
    # 获取依赖
    depend: List[str] = []
    for i in tree.imports:
        depend.append(str(i.path))
    data = DependData(name, path, depend)
    return data

# 解析文件





if __name__ == '__main__':
    controller_list = []
    service_list = []
    mapper_list = []
    dao_list = []
    finishing_depend_main(controller_list, service_list, mapper_list, dao_list)
